package com.jiawa.langchain4j.config;

import com.jiawa.langchain4j.aiservice.ConsultantService;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.spring.AiService;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Configuration
public class CommonConfig {

    @Autowired
    private OpenAiChatModel openAiChatModel;
    @Autowired
    private ChatMemoryStore chatMemoryStore;
    @Autowired
    private EmbeddingModel embeddingModel;
   // @Autowired
   // private RedisEmbeddingStore redisEmbeddingStore;
    /*@Bean
    public ConsultantService consultantService() {
        ConsultantService build = AiServices.builder(ConsultantService.class)
                .chatModel(openAiChatModel)
                .build();
        return build;
    }*/

    //构建会话记忆对象
    @Bean
    public ChatMemory chatMemory(){
        MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
                .maxMessages(20)
                .build();
        return memory;
    }

    //构建ChatMemoryProvider
    @Bean
    public ChatMemoryProvider  chatMemoryProvider(){
        ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() {
            @Override
            public ChatMemory get(Object memoryId) {
               return MessageWindowChatMemory.builder()
                       .id(memoryId)
                       .maxMessages(20)
                       .chatMemoryStore(chatMemoryStore)//配置redis
                       .build();
            }
        };
        return chatMemoryProvider;
    }

    @Bean
    //构建向量数据库操作
    public EmbeddingStore store(){
        //加载文档进内存
        //List<Document> document = ClassPathDocumentLoader.loadDocuments("content");
        //List<Document> document = FileSystemDocumentLoader.loadDocuments("F:\\JavaProject\\LangChain4j\\src\\main\\resources\\content");
        List<Document> document = ClassPathDocumentLoader.loadDocuments("content", new ApachePdfBoxDocumentParser());
        //构建向量数据库对象 内存版本的向量数据库
        InMemoryEmbeddingStore store = new InMemoryEmbeddingStore();



        //创建文档分割器对象
        DocumentSplitter documentSplitter= DocumentSplitters.recursive(500,100);
        //完成文本数据切割
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(store)
                .documentSplitter(documentSplitter)
                .embeddingModel(embeddingModel)
                //.batchSize(10)
                .build();
        ingestor.ingest(document);
        return store;
    }

    @Bean
    //构建向量数据库检索对象
    public ContentRetriever contentRetriever(EmbeddingStore embeddingStore){

        EmbeddingStoreContentRetriever build = EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .minScore(0.5)
                .maxResults(3)
                .embeddingModel(embeddingModel)
                .build();
        return build;
    }
}


